package net.neimeng.luculent.coremodel.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;

import net.neimeng.luculent.coremodel.R;

import java.util.ArrayList;
import java.util.List;


public class LoadingDialog extends ProgressDialog {

    private static List<LoadingDialog> dialogs = new ArrayList<>();

    private String hint;

    public LoadingDialog(Context context, int theme, String hint) {
        super(context, theme);
        this.hint = hint;
        dialogs.add(this);
    }

    public static LoadingDialog submitShow(Context context){

        return show(context,"数据提交中...");
    }

    public static LoadingDialog loadingShow(Context context){

        return show(context,"数据加载中...");
    }

    public static LoadingDialog show(Context context, String hint){
        LoadingDialog dialog = new LoadingDialog(context,R.style.common_dialog,hint);
        dialog.show();
        return dialog;
    }

    public static void cancelAll(){
        for (LoadingDialog dialog : dialogs){
            dialog.cancel();
        }
        dialogs.clear();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init(){
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.system_dialog_loading_process);//loading的xml文件
        TextView tv_message = findViewById(R.id.tv_message);
        tv_message.setText(hint);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
    }

}
